package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-05-29
 * Time: 12:07
 */

/**
 * 写一个方法，交换两个数
 */
//在栈上在开辟一个类的空间
class MyValue {
    public int val;
}
public class Test {
    //正确示范：
    public static void swap2(MyValue myV1, MyValue myV2) {
        int tmp = myV1.val;
        myV1.val = myV2.val;
        myV2.val = tmp;
    }
    public static void main(String[] args) {
        //对象的实例化
        MyValue myValue1 = new MyValue();
        MyValue myValue2 = new MyValue();
        //对MyValue类成员变量进行赋值
        myValue1.val = 10;
        myValue2.val = 20;
        System.out.println("交换前："+ myValue1.val + " " + myValue2.val);
        swap2(myValue1, myValue2);
        System.out.println("交换后："+ myValue1.val + " " + myValue2.val);
    }
    //错误示范：
    public static void swap(int x, int y) {
        int tmp = x;
        x = y;
        y = tmp;
    }
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println("交换前："+ a + " " + b);
        swap(a, b);
        System.out.println("交换后："+ a + " " + b);
    }
}
